/**
 * @(#) IncomeReportProxy.as
 *
 * @date		2010. 01. 17.
 * @version		1.0
 * @author		Goo,Sang Hoi
 * @description	 연말정산  - Proxy
 * 				신용카드 소득공제 신고서
 */
package com.skt.common_hr.yearadjustment.model
{

	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.NumberUtil;
	
	import flash.net.URLVariables;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	

	public class DonationReportProxy extends CommonProxy
	{
		public static const NAME:String = "DonationReportProxy";
		public var msgStr:String;
		
		public var donation:URLVariables = new URLVariables();
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function DonationReportProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 
		 * @param pernr 사번
		 * @return void
		 */
		public function load(pernr:String, year:String):void
		{
			//var loader:IDataLoader = DataLoaderFactory.getDataLoader("Z_HR_YEA_DONATION");
			var param:Object = new Object();						  

			param["I_PERNR"] = pernr;
			param["I_YEAR"] = year;

			loadData("Z_HR_YEA_DONATION", param, result, fault);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public function result(event:ResultEvent):void
		{
			trace( "suss to load Donation Report proxy");
			//Export 정보
			parseExport(event);
			//기부금 명세
			parseDonation(event.result.I_DONATION); 
			parseAdj(event.result.I_ADJ);
			parseSum(event.result.I_SUM);

			facade.sendNotification(YearAdjustmentFacade.LOAD_DONATION_REPORT_COMPLETE, this);
		}

/////////////////////////////////
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			trace("Fault message in Donation Report: " + event.fault.faultString);
			msgStr = event.fault.faultString;
			facade.sendNotification(YearAdjustmentFacade.LOAD_DONATION_REPORT_FAILED, this);
		}
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		
		/**
		 * 기부금 명세서  Export 정보
		 * 
		 * @param
		 * @return 기부금 명세서  Export 정보  Value Object
		 */
		private function parseExport(event:ResultEvent):void {
			donation["E_TEL"] = event.result["E_TEL"];
			donation["E_TEL1"] = event.result["E_TEL1"];
			
			donation["E_ENAME"] = event.result["E_ENAME"];
			donation["I_PERNR"] = Variables.yearPernr;
			//donation["E_BIRDT"] =  DateUtil.toFormatString(event.result["E_BIRDT"] as Date, "YYYY.MM.DD");
			// 2010.12.09 common infra에 맞게 수정 by Shin		
			var tmp:Date = DateUtil.stringToDateTime(event.result["E_BIRDT"]);
			donation["E_BIRDT"] = DateUtil.toDefaultString(tmp);
			donation["E_WORKPLACE"] = event.result["E_WORKPLACE"];
			//사업자 등록번호
			var bizNo:String = event.result["E_BIZNO"];
			donation["E_BIZNO"] = bizNo.substr(0, 3) + "-" + bizNo.substr(3, 2) + "-" +  bizNo.substr(5, 5);
			
			//주민번호
			var regNo:String = event.result["E_REGNO"];
			donation["E_REGNO"] = regNo.substr(0, 6) + "-" + regNo.substr(6, 7);
			
			donation["E_BIZPLACE"] = event.result["E_BIZPLACE"];
			donation["E_BIZPLACE2"] = event.result["E_BIZPLACE2"];
			
			donation["E_10"] = NumberUtil.NumberFormat(Number(event.result["E_10"]));
			donation["E_20"] = NumberUtil.NumberFormat(Number(event.result["E_20"]));
			donation["E_21"] = NumberUtil.NumberFormat(Number(event.result["E_21"]));
			donation["E_30"] = NumberUtil.NumberFormat(Number(event.result["E_30"]));
			donation["E_31"] = NumberUtil.NumberFormat(Number(event.result["E_31"]));
			donation["E_40"] = NumberUtil.NumberFormat(Number(event.result["E_40"]));
			donation["E_41"] = NumberUtil.NumberFormat(Number(event.result["E_41"]));
			donation["E_42"] = NumberUtil.NumberFormat(Number(event.result["E_42"]));
			donation["E_50"] = NumberUtil.NumberFormat(Number(event.result["E_50"]));
			
		}
		
		/**
		 * 기부금 명세서  정보
		 * 
		 * @param
		 * @return 기부금 명세서  Value Object
		 */
		private function parseDonation(arr:ArrayCollection):void
		{
			for (var i:int = 0; i<arr.length; ++i ){
				
				donation["ZTYPE" + i.toString()] = arr[i].ZTYPE;
				donation["ZCODE" + i.toString()] = arr[i].ZCODE;		
				donation["DESC" + i.toString()] = arr[i].DESC;
				donation["DDATE" + i.toString()] = arr[i].DDATE;
				donation["BIZTX" + i.toString()] = arr[i].BIZTX;
				
				//사업자 등록번호
				var bizNo:String = arr[i].BIZNO;
				if(bizNo.length == 10) {
					donation["BIZNO" + i.toString()] = bizNo.substr(0, 3) + "-" + bizNo.substr(3, 2) + "-" +  bizNo.substr(5, 5);
				}
				else {
					donation["BIZNO" + i.toString()] = bizNo.substr(0, 6) + "-" + bizNo.substr(6);
				}	
				donation["DOAMT" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].DOAMT));		
				donation["DOCNT" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].DOCNT));		
				donation["RELAT" + i.toString()] = arr[i].RELAT;
				donation["ENAME" + i.toString()] = arr[i].ENAME;
				//주민번호
				var regNo:String = arr[i].REGNO;
				donation["REGNO" + i.toString()] = regNo.substr(0, 6) + "-" + regNo.substr(6, 7);
				
			}

			donation["COUNT"] = arr.length.toString();				
		}

		private function parseAdj(arr:ArrayCollection):void
		{
			for (var i:int = 0; i<arr.length; ++i ){
				
				donation["CODE" + i.toString()] = arr[i].CODE;
				donation["YEAR" + i.toString()] = arr[i].YEAR;		
				donation["DON_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].DON_AMT);
				donation["PREV_DED_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].PREV_DED_AMT);
				donation["DEDUCTIBLE_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].DEDUCTIBLE_AMT);
				donation["CURR_DED_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].CURR_DED_AMT);
				donation["EXPIRED_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].EXPIRED_AMT);
				donation["CARRYOVER_AMT" + i.toString()] = NumberUtil.NumberFormat(arr[i].CARRYOVER_AMT);
			}

			donation["COUNTAdj"] = arr.length.toString();				
		}

		
		private function parseSum(arr:ArrayCollection):void
		{
			for (var i:int = 0; i<arr.length; ++i ){
				
				donation["LINE" + i.toString()] = arr[i].LINE;
				donation["TOTAL" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].TOTAL));	
				donation["M10" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M10));
				donation["M20" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M20));
				donation["M30" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M30));
				donation["M31" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M31));
				donation["M40" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M40));
				donation["M41" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M41));
				donation["M42" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M42));
				donation["M50" + i.toString()] = NumberUtil.NumberFormat(Number(arr[i].M50));
			}
			donation["SUM_COUNT"] = arr.length.toString();	
			
		}
		
		
	}
}